package com.lagou.email.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@RestController
@RequestMapping("/email")
public class LoginController {

    @Autowired
    JavaMailSender javaMailSender;


    @RequestMapping("/sendEmail")
    public String sendCode(@RequestParam("email") String email, @RequestParam("registCode") String registCode) {
        //将验证码和邮箱号保存到数据库
        sendMsgCodeToEmial(email, registCode);
        return "验证码已经发送至邮箱";
    }

   /* @RequestMapping("/register/{email}/{password}/{registCode}")
    public String register(@PathVariable String email, @PathVariable String password, @PathVariable String registCode){
        //使用邮箱查询出的验证码是否和传入的验证吗一致，一致  保存密码 和邮箱号
        MsgCode msgCode = loginService.searchMsgCode(email);
        String code = msgCode.getRegistCode();
        if(registCode.equals(code)){
            long l = System.currentTimeMillis() - msgCode.getSystime();
            long minu = l / (1000 * 60);
            if(minu > 5 ){
                return "验证码超时";
            }else{
                User user = loginService.searchUSER(email);
                if(user != null){
                    return "用户已存在";
                }else{
                    loginService.insertUSER(email,password);
                    return email+"注册成功";
                }
            }
        }else {
            return "验证码错误";
        }
    }


    @RequestMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, @PathVariable String registCode){
        //使用邮箱查询出的验证码是否和传入的验证吗一致，一致  保存密码 和邮箱号
        MsgCode msgCode = loginService.searchMsgCode(email);
        String code = msgCode.getRegistCode();
        if(registCode.equals(code)){
            long l = System.currentTimeMillis() - msgCode.getSystime();
            long minu = l / (1000 * 60);
            if(minu > 5 ){
                return "验证码超时";
            }else{
                User user = loginService.searchUSER(email);
                if(user != null){
                    return "用户已存在";
                }else{
                    loginService.insertUSER(email,password);
                    return email+"注册成功";
                }
            }
        }else {
            return "验证码错误";
        }*/









    

//"419716952@qq.com"
    private void sendMsgCodeToEmial(String  email,String  code) {
        //生成6位随机数
        System.out.println((int)((Math.random()*9+1)*100000));
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("注册验证码");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom(email);
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(email);
        // 设置邮件抄送人，可以有多个抄送人
        //message.setCc("12****32*qq.com");
        // 设置隐秘抄送人，可以有多个
        //message.setBcc("7******9@qq.com");
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText("注册验证码为:"+code+"(5分钟有效)");
        // 发送邮件
        javaMailSender.send(message);
    }

}
